using Gateway.IApplication.BaseInput;
using Gateway.IApplication.User;
using Gateway.IApplication.User.Input;
using Gateway.Infrastructure.Provider.Operator;
using Gateway.IQuery;
using Gateway.IQuery.Department;
using Gateway.IQuery.Department.Result;
using Gateway.IQuery.Role;
using Gateway.IQuery.Role.Result;
using Gateway.IQuery.User;
using Gateway.IQuery.User.Request;
using Gateway.IQuery.User.Result;
using Gateway.UI.Api.Filters.Attributes;

namespace Gateway.UI.Api.Controllers;

/// <summary>
/// 用户服务
/// </summary>
public class UserController : BaseController
{
    private readonly IUserApp _userApp;
    private readonly IUserQuery _userQuery;
    private readonly IDepartmentQuery _departmentQuery;
    private readonly IRoleQuery _roleQuery;
    private readonly IOperatorProvider _operatorProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="userApp"></param>
    /// <param name="userQuery"></param>
    /// <param name="departmentQuery"></param>
    /// <param name="roleQuery"></param>
    /// <param name="operatorProvider"></param>
    public UserController(IUserApp userApp, IUserQuery userQuery, IDepartmentQuery departmentQuery,
        IRoleQuery roleQuery, IOperatorProvider operatorProvider)
    {
        _userApp = userApp;
        _userQuery = userQuery;
        _departmentQuery = departmentQuery;
        _roleQuery = roleQuery;
        _operatorProvider = operatorProvider;
    }

    /// <summary>
    /// 创建用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("add")]
    public async Task<bool> Add(CreateUserInput input)
    {
        return await _userApp.Create(input);
    }

    /// <summary>
    /// 更新用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("update")]
    public async Task<bool> Update(UpdateUserInput input)
    {
        return await _userApp.Update(input);
    }

    /// <summary>
    /// 更新密码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("change/password")]
    public async Task<bool> ChangePassword(ChangePasswordInput input)
    {
        return await _userApp.ChangePassword(input);
    }
    /// <summary>
    /// 更新密码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("change/password/my")]
    public async Task<bool> ChangeMyPassword(ChangePasswordInput input)
    {
        input.UserId= _operatorProvider.GetOperator().OperatorId;
        return await _userApp.ChangePassword(input);
    }
    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("remove")]
    public async Task<bool> Remove(RemoveInput input)
    {
        return await _userApp.Remove(input.Id);
    }

    /// <summary>
    /// 获取用户分页列表
    /// </summary>
    /// <param name="request">请求参数</param>
    /// <returns></returns>
    [HttpPost, Route("list/page"), CheckApi]
    public async Task<PageResult<UserDetailResult>> GetPageList(UserPageListRequest request)
    {
        if (!string.IsNullOrWhiteSpace(request.DepartmentId))
        {
            var departmentIds = await _departmentQuery.GetDepartmentIdListByParentId(request.DepartmentId);
            if (departmentIds.Count > 0)
                request.DepartmentIds = departmentIds;
        }

        var data = await _userQuery.GetPageList(request);
        if (data.Rows.Count > 0)
        {
            List<string> roleIds = new List<string>();
            List<string> departmentIds = new List<string>();
            data.Rows.ForEach(p =>
            {
                if (!string.IsNullOrEmpty(p.RoleIds))
                {
                    roleIds.AddRange(p.RoleIds.Split(","));
                }

                if (!string.IsNullOrEmpty(p.DepartmentId))
                {
                    departmentIds.Add(p.DepartmentId);
                }
            });
            var roleList = new List<RoleSimpleResult>();
            //进行角色名称的赋值
            if (roleIds.Count > 0)
            {
                roleList = await _roleQuery.GetListByIds(roleIds);
            }

            var departmentList = new List<DepartmentDetailResult>();
            //进行所属部门的赋值
            if (departmentIds.Count > 0)
            {
                departmentList = await _departmentQuery.GetDetailByIds(departmentIds);
            }

            if (departmentList.Count > 0 || roleList.Count > 0)
            {
                data.Rows.ForEach(p =>
                {
                    p.RoleNames = new List<string>();
                    if (!string.IsNullOrEmpty(p.RoleIds))
                    {
                        var items = p.RoleIds.Split(",");
                        foreach (var item in items)
                        {
                            var info = roleList.FirstOrDefault(q => q.RoleId.Equals(item))?.RoleName;
                            if (!string.IsNullOrEmpty(info))
                            {
                                p.RoleNames.Add(info);
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(p.DepartmentId))
                    {
                        p.DepartmentName = departmentList.FirstOrDefault(q => q.DepartmentId.Equals(p.DepartmentId))
                            ?.DepartName;
                    }
                });
            }
        }

        return data;
    }


    /// <summary>
    /// 更新用户信息 现在只更新名称
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("set/my")]
    public async Task<bool> SetMyInfo(ChangeUserNameInput input)
    {
        input.UserId = _operatorProvider.GetOperator().OperatorId;
        return await _userApp.ChangeName(input);
    }
}